package cn.yueranzs.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yueranzs
 * @date 2021-11-01 11:40
 */
@Configuration
public class SwaggerConfig {

    //设置要显示的swagger环境
    Profiles profiles = Profiles.of("dev");

    @Bean
    public Docket docketCMJ(Environment environment) {
        //获取项目的测试环境：dev，为true即当前为测试环境，展示swagger
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)
                .groupName("cmj")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.yueranzs"))
                .build()
                //添加token至header
                .securityContexts(securityContexts())
                .securitySchemes(securitySchemes());
    }

    // 验证
    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> apiKeyList = new ArrayList();
        apiKeyList.add(new ApiKey("token", "token", "header"));
        return apiKeyList;
    }
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!auth).*$"))//过滤要验证的路径
                .build());
        return securityContexts;
    }
    //增加全局认证
    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("token", authorizationScopes));//验证增加（有许多教程说明中这个地方是Authorization,导致不能带入全局token，因为securitySchemes()方法中header写入token，所以这个地方我改为token就可以了）
        return securityReferences;
    }


    //重写swagger相关信息
    @Bean
    public ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("新冠物资管理系统", "https://www.yunseka.com", "");
        return new ApiInfo(
                "新冠物资管理系统",//大标题
                "新冠物资管理系统的接口预览",//内容
                "1.0", //版本
                "",//跳转地址
                contact,
                "",//开源版本
                "", //开源版本地址
                new ArrayList()
        );
    }
    
}
